import Taro from '@tarojs/taro'
import { baseUrl, noConsole } from '../config'

const request_data = {
    word: '戒烟',
    rn: 10,
    ic: 512,
    tn: 'resultjsonavstar'
}

export default (options = { method: 'GET', data: {} }) => {
    if (!noConsole) {
        console.log(`${new Date().toLocaleString()}【 M=${options.url} 】P=${JSON.stringify(options.data)}`)
    }
    return Taro.request({
        url: baseUrl + options.url,
        data: {
            ...request_data,
            ...options.data
        },
        // header: {
        //   'Content-Type': 'application/json',
        // },
        // mode:'cors',
        method: options.method.toUpperCase(),
    }).then(res => {
        const { statusCode, data } = res
        if (statusCode >= 200 && statusCode < 300) {
            if (!noConsole) {
                console.log(`${new Date().toLocaleString()}【 M=${options.url} 】【接口响应：】`, res.data)
            }
            if (data.status !== 'ok') {
                Taro.showToast({
                    title: `${res.data.error.message}~` || res.data.error.code,
                    icon: 'none',
                    mask: true,
                })
            }
            return data
        }
        throw new Error(`网络请求错误，状态码${statusCode}`)
    })
}